package com.hongxun.design.ui.dragview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import com.hongxun.design.R;
import com.hongxun.design.bean.PhotoPassagewayItemBean;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.utils.imageutil.UriUtil;


public class ImageTextItem extends LinearLayout  {
    private Context mContext = null;
    private SimpleDraweeView icon;
    private TextView name, value;
    private String touchUrl;


    private PhotoPassagewayItemBean itemBean=new PhotoPassagewayItemBean();



    public PhotoPassagewayItemBean getItemBean() {
        return itemBean;
    }



    public ImageTextItem(Context context) {
        super(context);
        MyLog.L("进入ImageTextItem(Context context)");
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View parentView = inflater.inflate(R.layout.ui_imagetextitem, this, true);
        icon = (SimpleDraweeView) parentView.findViewById(R.id.passageway_icon);
        name = (TextView) parentView.findViewById(R.id.passageway_name);
        value = (TextView) parentView.findViewById(R.id.passageway_value);
    }

    public ImageTextItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        MyLog.L("进入ImageTextItem(Context context, AttributeSet attrs)");
        mContext = context;

//        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        View parentView = inflater.inflate(R.layout.ui_imagetextitem, this, true);
//        icon = (SimpleDraweeView) parentView.findViewById(R.id.passageway_icon);
//        name = (TextView) parentView.findViewById(R.id.passageway_name);
//        value = (TextView) parentView.findViewById(R.id.passageway_value);

    }

    @Override
    protected void onFinishInflate() {
//        icon = (ImageView) findViewById(R.id.passageway_icon);
//        name = (TextView)  findViewById(R.id.passageway_name);
//        value = (TextView) findViewById(R.id.passageway_value);

        super.onFinishInflate();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }


    public  void setData(PhotoPassagewayItemBean itemBean,int is){
        this.itemBean=itemBean;
        touchUrl = itemBean.getUrlSeleck();
        if (icon != null) {
             icon.setImageURI(UriUtil.getUri(itemBean.getUrl()));
        }

        if (name != null) {
            name.setText(itemBean.getName());
        }
        if (value != null) {
            value.setText(itemBean.getValue());
             value.setVisibility(is);

        }
    }
    public void setIcon(String url) {
        if (icon != null) {
            MyLog.L("进入ImageTextItem(Context context)：设置url" + url + "  icon=" + icon);
            icon.setImageURI(UriUtil.getUri(url));
            itemBean.setUrl(url);
        }
    }
    public void setImage (String url) {
        if (icon != null) {
            MyLog.L("进入ImageTextItem(Context context)：设置url" + url + "  icon=" + icon);
            icon.setImageURI(UriUtil.getUri(url));

        }
    }

    public void setIconTouch(String url) {
        touchUrl = url;
        itemBean.setUrlSeleck(url);
    }

    public String getIconTouch() {
        return touchUrl + "";
    }


    public void setName(String title) {
        if (name != null) {
            itemBean.setName(title);
            name.setText(title);
        }
    }


    public void setValue(String v, int is) {
        if (value != null) {
            value.setText(v);
            itemBean.setValue(v);
            value.setVisibility(is);

        }
    }
    public void setGone(){
        name.setVisibility(GONE);
        value.setVisibility(GONE);
    }

//    private int _xDelta;
//    private int _yDelta;
//    @Override
//    public boolean onTouch(View view, MotionEvent event) {
//        final int X = (int) event.getRawX();
//        final int Y = (int) event.getRawY();
//        switch (event.getAction() & MotionEvent.ACTION_MASK) {
//            case MotionEvent.ACTION_DOWN:
//                MyLog.L("触摸grid:move:"+((MyPassagewayDragView)view.getParent()).move );
//                ((MyPassagewayDragView)view.getParent()).move.setVisibility(VISIBLE);
//                ((MyPassagewayDragView)view.getParent()).move.setImageURI(UriUtil.getUri(touchUrl));
//                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) ((MyPassagewayDragView)view.getParent()).move
//                        .getLayoutParams();
//                _xDelta = X - lParams.leftMargin;
//                _yDelta = Y - lParams.topMargin;
//                break;
//            case MotionEvent.ACTION_UP:
//                break;
//            case MotionEvent.ACTION_POINTER_DOWN:
//                break;
//            case MotionEvent.ACTION_POINTER_UP:
//                break;
//            case MotionEvent.ACTION_MOVE:
//                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ((MyPassagewayDragView)view.getParent()).move
//                        .getLayoutParams();
//                layoutParams.leftMargin = X - _xDelta;
//                layoutParams.topMargin = Y - _yDelta;
//                layoutParams.rightMargin = -250;
//                layoutParams.bottomMargin = -250;
//                view.setLayoutParams(layoutParams);
//                break;
//        }
//        ((MyPassagewayDragView)view.getParent()).move.invalidate();
//        return true;
//    }
}
